<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class ProjectRecord extends MY_Controller {
	public function __construct(){
		parent::__construct();
		$this->isLogin();
		$this->load->model('ProjectRecord_model');
	}
	public function projectRecordList(){
		//判断如果是研发人员查询
		if($this->pmsuserinfo['isitadministrator']!=1 && intval($this->pmsuserinfo['type'])===0){
			$powerlist=intval($this->pmsuserinfo['powerlist']);
			if(!($powerlist & 4) && !($powerlist & 8) && !($powerlist & 16)){
				$this->projectRecordListYf();
				exit;
			}
		}
		//获取项目
		$prolist=$this->ProjectRecord_model->getProjectAllList('id,name');
		$newprolist=array();
		if(!empty($prolist)){
			foreach ($prolist as $k => $v) {
				$newprolist[$v['id']]=$v['name'];
			}
		}
		$this->assign('prolist',$prolist);
		//查询所有客户
		$custmoerlist=$this->ProjectRecord_model->getCustmoerAllList('id,name');
		$newcustmoerlist=array();
		if(!empty($custmoerlist)){
			foreach ($custmoerlist as $k => $v) {
				$newcustmoerlist[$v['id']]=$v['name'];
			}
		}
		$this->assign('custlist',$custmoerlist);
		$where=array();	
		if(isset($_POST['isall']) && intval($_POST['isall'])==0){
			
		}
		if(isset($_POST['prjid'])){
			$where['prjid']=array('=',$_POST['prjid']);
		}
		if(isset($_POST['customer_id'])){
			$where['customer_id']=array('=',$_POST['customer_id']);
		}
		if(isset($_POST['prjstaus'])){
			$where['prjstaus']=array('=',$_POST['prjstaus']);
		}
		if(isset($_POST['status'])){
			$status=$_POST['status'];
			if($status==999){
				$where['status']=array('!=',100);
			}elseif($status!=998){
				$where['status']=array('=',$_POST['status']);
			}
		}else{
			$where['status']=array('!=',100);
		}
		if(isset($_POST['isquick'])){
			$where['isquick']=array('=',$_POST['isquick']);
		}
		//判断当前用户是否是销售
		if($this->pmsuserinfo['isitadministrator']!=1 && $this->pmsuserinfo['type']==1){
			$powerlist=intval($this->pmsuserinfo['powerlist']);
			if(!($powerlist & 4) && !($powerlist & 8) && !($powerlist & 16)){
				$where['saleid']=array('=',$this->pmsuserinfo['id']);
			}
		}
		$result=$this->ProjectRecord_model->pm_projectrecord_ajax_list($where);
		//echo $this->db->last_query();
		$useridarr=array();
		if(!empty($result['list'])){
			foreach ($result['list'] as $k => $v) {
				$useridarr[]=$v['adduid'];
				$v['projectname']='';
				$v['custmoername']='';
				if(isset($newprolist[$v['prjid']])){
					$v['projectname']=$newprolist[$v['prjid']];
				}
				if(isset($newcustmoerlist[$v['customer_id']])){
					$v['custmoername']=$newcustmoerlist[$v['customer_id']];
				}
				$result['list'][$k]=$v;
			}
		}
		//获取用户信息
		$newuserlist=array();
		if(!empty($useridarr)){
			$userlist=$this->ProjectRecord_model->getUserInfo($useridarr,'id,displayname');
			if($userlist){
				foreach ($userlist as $k => $v) {
					$newuserlist[$v['id']]=$v['displayname'];
				}
			}
		}
		if(!empty($result['list'])){
			foreach ($result['list'] as $k => $v) {
				$v['username']='';
				if(isset($newuserlist[$v['adduid']])){
					$v['username']=$newuserlist[$v['adduid']];
				}
				$result['list'][$k]=$v;
			}
		}
		$this->assign('list',$result['list']);
		$this->assign('pageshow',$result['pageshow']);
		if(isset($_POST['page'])){
			$html=$this->fetch('projectrecord/list_ajax.html');
			$result['html']=$html;
			ajax_return(1,'success',$result);
		}
		$this->display('projectrecord/list.html');
	}
	public function projectRecordListYf(){
		//获取项目
		$prolist=$this->ProjectRecord_model->getProjectAllList('id,name');
		$newprolist=array();
		if(!empty($prolist)){
			foreach ($prolist as $k => $v) {
				$newprolist[$v['id']]=$v['name'];
			}
		}
		$this->assign('prolist',$prolist);
		//查询所有客户
		$custmoerlist=$this->ProjectRecord_model->getCustmoerAllList('id,name');
		$newcustmoerlist=array();
		if(!empty($custmoerlist)){
			foreach ($custmoerlist as $k => $v) {
				$newcustmoerlist[$v['id']]=$v['name'];
			}
		}
		$this->assign('custlist',$custmoerlist);
		$where=array();		
		if(isset($_POST['prjid'])){
			$where['A.prjid']=array('=',$_POST['prjid']);
		}
		if(isset($_POST['customer_id'])){
			$where['A.customer_id']=array('=',$_POST['customer_id']);
		}
		if(isset($_POST['prjstaus'])){
			$where['A.prjstaus']=array('=',$_POST['prjstaus']);
		}
		if(isset($_POST['status'])){
			$status=$_POST['status'];
			if($status==999){
				$where['status']=array('!=',100);
			}elseif($status!=998){
				$where['status']=array('=',$_POST['status']);
			}
		}else{
			$where['status']=array('!=',100);
		}
		if(isset($_POST['isquick'])){
			$where['A.isquick']=array('=',$_POST['isquick']);
		}
		$where['B.yfid']=array('=',$this->pmsuserinfo['id']);
		$result=$this->ProjectRecord_model->pm_projectrecord_ajax_list_yf($where);
		$useridarr=array();
		if(!empty($result['list'])){
			foreach ($result['list'] as $k => $v) {
				$useridarr[]=$v['adduid'];
				$v['projectname']='';
				$v['custmoername']='';
				if(isset($newprolist[$v['prjid']])){
					$v['projectname']=$newprolist[$v['prjid']];
				}
				if(isset($newcustmoerlist[$v['customer_id']])){
					$v['custmoername']=$newcustmoerlist[$v['customer_id']];
				}
				$result['list'][$k]=$v;
			}
		}
		//获取用户信息
		$newuserlist=array();
		if(!empty($useridarr)){
			$userlist=$this->ProjectRecord_model->getUserInfo($useridarr,'id,displayname');
			if($userlist){
				foreach ($userlist as $k => $v) {
					$newuserlist[$v['id']]=$v['displayname'];
				}
			}
		}
		if(!empty($result['list'])){
			foreach ($result['list'] as $k => $v) {
				$v['username']='';
				if(isset($newuserlist[$v['adduid']])){
					$v['username']=$newuserlist[$v['adduid']];
				}
				$result['list'][$k]=$v;
			}
		}
		$this->assign('list',$result['list']);
		$this->assign('pageshow',$result['pageshow']);
		if(isset($_POST['page'])){
			$html=$this->fetch('projectrecord/list_ajax.html');
			$result['html']=$html;
			ajax_return(1,'success',$result);
		}
		$this->display('projectrecord/list.html');
	}
	public function projectRecordAdd(){
		if(isset($_POST['prjid'])){
			$data=$_POST;
			$projd=$_POST['prjid'];
			//根据项目id获取项目信息
			$prolist=$this->ProjectRecord_model->getProjectList($projd,'id,custmoerid,status,saleid');
			$data['customer_id']=$prolist['custmoerid'];
			$data['prjstaus']=$prolist['status'];
			$data['saleid']=$prolist['saleid'];
			$data['status']=0;//新建
			$recordaddid=$this->ProjectRecord_model->pm_project_record_add($data);
			if($recordaddid===false){
				$keywords='创建项目进度状况.失败';
				useraddrecord('项目进度',$keywords);
				ajax_return(0,'提交失败');
			}else{
				$keywords='创建项目进度状况ID:'.$recordaddid.'成功';
				useraddrecord('项目进度',$keywords);
				$changedata['id']=$recordaddid;
				$changedata['type']=0;
				$changedata['customer_id']=$prolist['custmoerid'];
				$changedata['status']=1;
				$changedata['remark']="创建项目任务";
				$this->ProjectRecord_model->writeProjectChange($changedata);
				ajax_return(1,'success');
			}
		}else{
			//查询项目
			$prolist=$this->ProjectRecord_model->getProjectAllList('id,name');
			if(empty($prolist)){
				echo Js::alert('请先添加项目');exit;
			}
			$this->assign('prolist',$prolist);
			$this->display('projectrecord/add.html');
		}
	}
	public function projectRecordEdit(){
		if(isset($_POST['id'])){
			$data=$_POST;
			$data['status']=0;
			$recordaddid=$this->ProjectRecord_model->pm_project_record_edit($data);
			if($recordaddid===false){
				$keywords='编辑项目进度状况.失败';
				useraddrecord('项目进度',$keywords);
				ajax_return(0,'提交失败');
			}else{
				$keywords='编辑项目进度状况ID:'.$recordaddid.'成功';
				useraddrecord('项目进度',$keywords);
				ajax_return(1,'success');
			}
		}elseif(isset($_GET['id'])){
			//获取项目
			$prolist=$this->ProjectRecord_model->getProjectAllList('id,name');
			$this->assign('prolist',$prolist);
			$list=$this->ProjectRecord_model->getPmProjectRecordOne($_GET['id']);
			if(!$list){
				echo Js::alert('不存在信息');exit;
			}
			//获取附件
			$getattachmentlist=$this->ProjectRecord_model->getattachmentlist($list['id'],'id,filepath');
			$attachmentlistarr=array();
			if(!empty($getattachmentlist)){
				foreach ($getattachmentlist as $k => $v) {
					$v['filename']=basename(".".$v['filepath']);
					$v['download']=PMSROOT.$v['filepath'];
					$attachmentlistarr[]=$v['filepath'];
					$getattachmentlist[$k]=$v;
				}
			}
			$attachmentliststr='';
			if(!empty($attachmentlistarr)){
				$attachmentliststr=implode(',',$attachmentlistarr);
			}
			$this->assign('attachmentliststr',$attachmentliststr);
			$this->assign('getattachmentlist',$getattachmentlist);
			$this->assign('list',$list);
			$this->display('projectrecord/edit.html');
		}
	}
	//项目进度详情
	public function projectRecordeDetails(){
		if(isset($_GET['id'])){
			//获取项目
			$id=$_GET['id'];
			$list=$this->ProjectRecord_model->getPmProjectRecordOne($id);
			$listProject=$this->ProjectRecord_model->getProjectList($list['prjid']);
			$list['projectname']=$listProject['name'];
			$list['custmoername']='';
			$list['saleidname']='';
			$list['technologyuidname']='';
			$list['yflistname']='';
			//获取研发人员id集合
			$yflist=$this->ProjectRecord_model->getPmProjectRecordDeveloping($id,'yfid');
			$yfidarr=array();
			if(!empty($yflist)){
				foreach($yflist as $k=>$v){
					$yfidarr[]=$v['yfid'];
				}
			}
			// 销售 技术人名称
			$userarr=array();
			$userarr[]=$listProject['saleid'];
			$userarr[]=$listProject['technologyuid'];
			$userarr=array_merge($userarr,$yfidarr);
			$userarr=array_unique($userarr);
			$userlistnew=array();
			$userlist=$this->ProjectRecord_model->getUserInfo($userarr,'id,displayname');
			if(!empty($userlist)){
				foreach($userlist as $k=>$v){
					$userlistnew[$v['id']]=$v['displayname'];
				}
			}
			if(isset($userlistnew[$listProject['saleid']])){
				$list['saleidname']=$userlistnew[$listProject['saleid']];
			}
			if(isset($userlistnew[$listProject['technologyuid']])){

				$list['technologyuidname']=$userlistnew[$listProject['technologyuid']];
			}
			//获取研发名称
			$newyfnamearr=array();
			if(!empty($yfidarr)){
				foreach($yfidarr as $k=>$v){
					if(isset($userlistnew[$v])){
						$newyfnamearr[]=$userlistnew[$v];
					}
				}
			}
			if(!empty($newyfnamearr)){
				$list['yflistname']=implode(',',$newyfnamearr);
			}
			//获取客服
			$customer_id_arr=array();
			$customer_id_arr[]=$listProject['custmoerid'];
			$custmoerlist=$this->ProjectRecord_model->getCustmoerInfo($customer_id_arr);
			$custlistinfo=array();
			if(isset($custmoerlist[0])){
				$custlistinfo[$custmoerlist[0]['id']]=$custmoerlist[0]['name'];
			}
			if(isset($custlistinfo[$listProject['custmoerid']])){
				$list['custmoername']=$custlistinfo[$listProject['custmoerid']];
			}
			
			
			//获取附件
			
			$getattachmentlist=$this->ProjectRecord_model->getattachmentlist($list['id'],'id,filepath');
			$attachmentlistarr=array();
			if(!empty($getattachmentlist)){
				foreach ($getattachmentlist as $k => $v) {
					$v['filename']=basename(".".$v['filepath']);
					$v['download']=PMSROOT.$v['filepath'];
					$attachmentlistarr[]=$v['filepath'];
					$getattachmentlist[$k]=$v;
				}
			}
			$attachmentliststr='';
			if(!empty($attachmentlistarr)){
				$attachmentliststr=implode(',',$attachmentlistarr);
			}
			$this->assign('attachmentliststr',$attachmentliststr);
			$this->assign('getattachmentlist',$getattachmentlist);
			$this->assign('list',$list);
			$this->display('projectrecord/details.html');
		}
	}
	public function projectRrcordVerify(){
		if(isset($_POST['id']) && isset($_POST['verify']) && isset($_POST['remark'])){
			$recordid=$id=$_POST['id'];
			$verify=$_POST['verify'];
			$remark=$_POST['remark'];
			$yflist=$_POST['yflist'];
			$list=$this->ProjectRecord_model->getPmProjectRecordOne($id,'status,customer_id,prjid');
			$status=array('0','1','2','3','100');
			if(!in_array($list['status'],$list)){
				ajax_return(0,'数据参数不能用户审核');
			}
			//注释描述获取
			if($verify==1 || $verify==19){
				$changeremark="项目经理签字";
				if($this->pmsuserinfo['isitadministrator']!=1){//不是超级管理员
					//判断当前项目经理审核不能审核不是自己项目
					$procur_list=$this->ProjectRecord_model->getProjectList($list['prjid'],'saleid,id');
					if(!$procur_list){
						ajax_return(0,'当前任务获取项目信息失败');
					}
					if($procur_list['saleid']!=$this->pmsuserinfo['id']){
						ajax_return(0,'当前任务不能替审');
					}
				}
			}elseif($verify==2 || $verify==29){
				$changeremark="总经理签字";
			}elseif($verify==3 || $verify==39){
				if($verify==3 && $yflist==''){
					ajax_return(0,'请选择研发人员');
				}
				$changeremark="研发签字";
			}elseif($verify==100){
				$changeremark="完成签字";
			}elseif($verify==99){
				$changeremark="由完成状态".$list['status']."设置为研发通过状态3";
			}
			//操作状态获取
			if(in_array($verify,array(0,1,2,3,100,99))){
				$changestatus=1;
				$changestatus_key="通过";
			}else{
				$changestatus=2;
				$changestatus_key='驳回';
			}
			if($this->pmsuserinfo['isitadministrator']!=1){//不是超级管理员
				if($verify==1 || $verify==19){
					if(!($this->pmsuserinfo['powerlist'] & 2)){
						ajax_return(0,'没有权限2');
					}
				}elseif($verify==2 || $verify==29){
					if(!($this->pmsuserinfo['powerlist'] & 4)){
						ajax_return(0,'没有权限4');
					}
				}elseif($verify==3 || $verify==39){
					if(!($this->pmsuserinfo['powerlist'] & 8)){
						ajax_return(0,'没有权限8');
					}
				}elseif($verify==100){
					if(!($this->pmsuserinfo['powerlist'] & 16)){
						ajax_return(0,'没有权限16');
					}
				}elseif($verify==99){
					if(!($this->pmsuserinfo['powerlist'] & 16)){
						ajax_return(0,'没有权限16');
					}
				}
			}
			if($verify==99){//审核为完成转研发
				$verify=3;
			}
			if($this->ProjectRecord_model->pm_project_record_edit_verify($id,$verify)===false){
				$keywords='项目进度'.$changeremark.$changestatus_key.'失败';
				useraddrecord('项目进度',$keywords);
				ajax_return(0,'提交失败');
			}else{
				$changedata['id']=$id;
				if($list['status']==0){
					$changedata['type']=1;
				}elseif($list['status']==1){
					$changedata['type']=2;
				}elseif($list['status']==2){
					$changedata['type']=3;
				}elseif($list['status']==3){
					$changedata['type']=100;
				}elseif($list['status']==100){
					$changedata['type']=3;
				}
				$changedata['customer_id']=$list['customer_id'];
				$changedata['status']=$changestatus;
				$changedata['remark']=$remark;
				//$changedata['yflist']=$yflist;
				$this->ProjectRecord_model->writeProjectChange($changedata);
				//写入研发用户信息
				if($verify==3){
					$yflist=explode(',',$yflist);
					$this->ProjectRecord_model->writePmProjectRecordDeveloping($recordid,$yflist);
				}
				$keywords='项目任务ID'.$recordid.",".$changeremark.$changestatus_key.'成功';
				useraddrecord('项目进度',$keywords);
				ajax_return(1,'success');
			}
		}
	}
	//项目进度记录
	public function projectRecordLog(){
		if(isset($_GET['id'])){
			$id=$_GET['id'];
			//获取项目信息
			$recordlist=$this->ProjectRecord_model->getPmProjectRecordOne($id,'prjid');
			$projectlist=$this->ProjectRecord_model->pm_project_one($recordlist['prjid']);
			$this->assign('projectlist',$projectlist);
			$list=$this->ProjectRecord_model->pm_projectrecord_log_list($id);
			$userarr=array();
			$userarrnew=array();
			$customeridarr=array();
			$customeridarrnew=array();
			if(!empty($list)){
				foreach($list as $k=>$v){
					$customeridarr[]=$v['customer_id'];
					$userarr[]=$v['adduid'];
				}
			}
			
			
			if(!empty($userarr)){
				$userarr=array_unique($userarr);
				$userlist=$this->ProjectRecord_model->getUserInfo($userarr,'id,displayname');
				if(!empty($userlist)){
					foreach($userlist as $k=>$v){
						$userarrnew[$v['id']]=$v['displayname'];
					}
				}
			}
			if(!empty($customeridarr)){
				$customeridarr=array_unique($customeridarr);
				$customerlist=$this->ProjectRecord_model->getCustmoerInfo($customeridarr,'id,name');
				if(!empty($customerlist)){
					foreach($customerlist as $k=>$v){
						$customeridarrnew[$v['id']]=$v['name'];
					}
				}
			}
			foreach($list as $k=>$v){
				$v['custmoername']='';
				$v['username']='';
				if(isset($customeridarrnew[$v['customer_id']])){
					$v['custmoername']=$customeridarrnew[$v['customer_id']];
				}
				if(isset($userarrnew[$v['adduid']])){
					$v['username']=$userarrnew[$v['adduid']];
				}
				$list[$k]=$v;
			}
			$this->assign('list',$list);
			$this->display('projectrecord/recordlogslist.html');
		}
	}
	public function projectRecordVerifyHtml(){
		if(isset($_GET['id'])){
			$id=$_GET['id'];
			$list=$this->ProjectRecord_model->getPmProjectRecordOne($id,'id,status,customer_id');
			$this->assign('list',$list);
			///查询所有研发人员
			$yanfauserlist=$this->ProjectRecord_model->getYanFaUserList();
			$this->assign('yanfauserlist',$yanfauserlist);
			$this->display('projectrecord/verifyhtml.html');
		}
	}
	//研发名单
	public function projectYfInfo(){
		if(isset($_GET['id'])){
			$id=$_GET['id'];
			$list=$this->ProjectRecord_model->getPmProjectRecordDeveloping($id);
			//获取研发名称
			$yfidarr=array();
			if($list){
				foreach($list as $k=>$v){
					$yfidarr[]=$v['yfid'];
					$yfidarr[]=$v['adduid'];
				}
			}
			$newuserinfo=array();
			if(!empty($yfidarr)){
				$yfidarr=array_unique($yfidarr);
				$userlist=$this->ProjectRecord_model->getUserInfo($yfidarr,'id,name,displayname');
				foreach($userlist as $k=>$v){
					$newuserinfo[$v['id']]=$v;
				}
			}
			if($list){
				foreach($list as $k=>$v){
					$v['username']='';
					$v['adminname']='';
					if(isset($newuserinfo[$v['yfid']])){
						$v['username']=$newuserinfo[$v['yfid']]['displayname'];
					}
					if(isset($newuserinfo[$v['adduid']])){
						$v['adminname']=$newuserinfo[$v['adduid']]['displayname'];
					}
					$list[$k]=$v;
				}
			}
			$this->assign('list',$list);
			$this->display('projectrecord/yfinfo.html');
		}
	}
	//删除
	public function projectRecordDel(){
		if(isset($_POST['id'])){
			$id=$_POST['id'];
			$prorecordlist=$this->ProjectRecord_model->getPmProjectRecordOne($id,'status');
			if(!$prorecordlist){
				ajax_return(0,'参数错误');
			}elseif($prorecordlist['status']!=0){
				ajax_return(0,'项目任务不是新建模式不能删除');
			}else{
				if($this->ProjectRecord_model->delPmProjectRecord($id)===false){
					$keywords='项目任务ID'.$id.'删除失败';
					useraddrecord('项目进度',$keywords);
					ajax_return(0,'删除失败');
				}else{
					$keywords='项目任务ID'.$id.'删除成功';
					useraddrecord('项目进度',$keywords);
					ajax_return(1,'删除成功');
				}
			}
		}
	}
}

?>